<?php

/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/2/24
 * Time: 11:20
 */
class Weixindevice extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->config->load('weixin');
        $this->_appid = $this->config->item('nldou_fuwu_appid');
        $this->_appsecret = $this->config->item('nldou_fuwu_appsecret');
        $this->_wx_type = $this->config->item('nldou_fuwu_type');
    }

    private function getSourceConfig($source){
        $this->config->load('weixin');
        $this->_appid = $this->config->item($source.'_appid');
        $this->_appsecret = $this->config->item($source.'_appsecret');
        $this->_wx_type = $this->config->item($source.'_type');
    }

    //查询已在微信注册ibeacon设备列表
    public function wx_device_search()
    {
        $data = '{
				    "type": 2,
				    "last_seen": 0,		
				    "count": 10
                 }';

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->wx_device_search($data);
        //返回示例
        /*{
         "data": {
            "devices": [
                {
                "comment": "",
                "device_id": 10097,
                "major": 10001,
                "minor": 12102,
                "status": 1,
                "last_active_time":1437276018,
                "poi_id": 0,
                "uuid": "FDA50693-A4E2-4FB1-AFCF-C6EB07647825"
                },
                .....
             ],
            "total_count": 151
          },
          "errcode": 0,
          "errmsg": "success."
        }*/
        var_dump($res);
    }

    //新增微信注册ibeacon设备分组
    public function add_wx_device_group()
    {
        if(isset($_POST['group_name'])){
            $name = $_POST['group_name'];
        }else{
            $this->error('缺少分组名称参数');
        }
        //var_dump($name);exit;
        $data = array('group_name'=>$name);
        $data = json_encode($data);

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->add_wx_device_group($data);
        //返回示例
        /*{
              "data": {
                  "group_id" : 123,
                  "group_name" : "test"
              },
              "errcode": 0,
              "errmsg": "success."
        }*/
        $this->success($res);
    }

    //修改微信注册ibeacon设备分组名
    public function edit_wx_device_group_name()
    {
        if(isset($_POST['group_name']) && isset($_POST['group_id'])){
            $name = $_POST['group_name'];
            $id   = $_POST['group_id'];
        }else{
            $this->error('缺少分组名称参数');
        }

        $data = array('group_name'=>$name,'group_id'=>(int)$id);
        $data = json_encode($data);
        /*请求示例
         {
            "group_id":123456,
            "group_name":"test"
         }*/

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->edit_wx_device_group_name($data);
        //返回示例
        /*{
            "data": {
            },
            "errcode": 0,
            "errmsg": "success."
        }*/
        //var_dump($res);
        $this->success($res);
    }

    //删除修改微信注册ibeacon设备分组
    public function delete_wx_device_group()
    {
        if(isset($_POST['group_id'])){
            $id   = $_POST['group_id'];
        }else{
            $this->error('缺少分组ID参数');
        }

        $data = array('group_id' => (int)$id);
        $data = json_encode($data);
        /*请求示例
         {
           "group_id":123456
         }*/

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->delete_wx_device_group($data);
        //返回示例
        /*{
            "data": {
            },
            "errcode": 0,
            "errmsg": "success."
         }*/
        //var_dump($res);
        $this->success($res);
    }

    //从分组中移除设备
    public function delete_wx_device_from_group()
    {
        //注意数据类型转换
        if(isset($_POST['group_id']) && isset($_POST['device_id'])
            && isset($_POST['uuid']) && isset($_POST['major']) && isset($_POST['minor']))
        {
            $grpid = (int)$_POST['group_id'];
            $devid = (int)$_POST['device_id'];
            $uuid  = $_POST['uuid'];
            $major = (int)$_POST['major'];
            $minor = (int)$_POST['minor'];
        }else{
            $this->error('参数不全');
        }

        //注意数据类型转换
        $data = array('group_id'=>$grpid,'device_identifiers'=>
            array(
                array('device_id'=>$devid,'uuid'=>$uuid,'major'=>$major,'minor'=>$minor)
            )
        );
        //var_dump($data);exit;
        $data = json_encode($data);
        //请求示例
        /*{
              "group_id": 2569121,
              "device_identifiers":[
                  {
                  "device_id":8224142,
                  "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
                  "major":10090,
                  "minor":4460
                  }
               ]
           }*/

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->delete_wx_device_from_group($data);
        //返回示例
        /*{
            "data": {
            },
            "errcode": 0,
            "errmsg": "success."
         }*/
        $this->success($res);
    }
    //新增微信注册ibeacon设备到分组
    public function add_wx_device_to_group()
    {
        //注意数据类型转换
        if(isset($_POST['group_id']) && isset($_POST['device_id'])
            && isset($_POST['uuid']) && isset($_POST['major']) && isset($_POST['minor']))
        {
            $grpid = (int)$_POST['group_id'];
            $devid = (int)$_POST['device_id'];
            $uuid  = $_POST['uuid'];
            $major = (int)$_POST['major'];
            $minor = (int)$_POST['minor'];
        }else{
            $this->error('参数不全');
        }

        $data = array('group_id'=>$grpid,'device_identifiers'=>
            array(
                array('device_id'=>$devid,'uuid'=>$uuid,'major'=>$major,'minor'=>$minor)
            )
        );

        $data = json_encode($data);
        //请求示例
        /*{
              "group_id": 2569121,
              "device_identifiers":[
                  {
                  "device_id":8224142,
                  "uuid":"FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
                  "major":10090,
                  "minor":4460
                  }
               ]
           }*/

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->add_wx_device_to_group($data);
        //返回示例
        /*{
            "data": {
            },
            "errcode": 0,
            "errmsg": "success."
         }*/
        $this->success($res);
    }
    //查询微信注册ibeacon设备分组列表
    public function wx_device_group_list()
    {
        $data = '{
    			   "begin": 0,
                   "count": 10
                 }';

        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->wx_device_group_list($data);
        //返回示例
        /*{
            "data": {
                "total_count": 100,
                "groups":[
                    {
                       "group_id" : 123,
                       "group_name" : "test1"
                    },
                    ....
                ]
             },
            "errcode": 0,
            "errmsg": "success."
         }*/
        if($res['errcode'] == 0){
            echo "total_count => ".$res['data']['total_count']."<br>";
            foreach ($res['data']['groups'] as $k=>$v){
                echo "<hr>group".$k."<br>group_id => ".$v['group_id']."<br>group_name => ".$v['group_name'];
            }
        }else{
            echo "errcode => ".$res['errcode']."<br>errmsg => ".$res['errmsg'];
        }

        //var_dump($res);
    }
    //查询微信注册ibeacon设备分组详情
    public function wx_device_group_detail()
    {
        if(isset($_GET['grpid'])){
            $grpid = (int)$_GET['grpid'];
        }else{
            $this->error('缺少设备组id');
        }
        $data = array('group_id'=>$grpid,'begin'=>0,'count'=>10);
        $data = json_encode($data);
        //请求示例
        /*{
          "group_id":2569121,
          "begin":0,
          "count":10
        }*/
        $this->load->library("wxinterface/Wxinterface",array("appid"=>$this->_appid,"appsecret"=>$this->_appsecret,"wxtype"=>$this->_wx_type));
        $res=$this->wxinterface->wx_device_group_detail($data);
        //返回示例
        /*{
              "data": {
                  "group_id" : 123,
                  "group_name" : "test",
                  "total_count": 100,
                  "devices" :[
                      {
                      "device_id" : 123456,
                      "uuid" : "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
                      "major" : 10001,
                      "minor" : 10001,
                      "comment" : "test device1",
                      "poi_id" : 12345,
                      },
                      {
                      "device_id" : 123457,
                      "uuid" : "FDA50693-A4E2-4FB1-AFCF-C6EB07647825",
                      "major" : 10001,
                      "minor" : 10002,
                      "comment" : "test device2",
                      "poi_id" : 12345,
                      }
                      .....
                      ]
                     },
              "errcode": 0,
              "errmsg": "success."
           }*/
        if($res['errcode'] == 0){
            echo "group_id => ".$res['data']['group_id']."<br>";
            echo "group_name => ".$res['data']['group_name']."<br>";
            echo "total_count => ".$res['data']['total_count']."<br>";
            foreach ($res['data']['devices'] as $k=>$v){
                echo "<hr>device".$k
                    ."<br>device_id => ".$v['device_id']
                    ."<br>uuid => ".$v['uuid']
                    ."<br>major => ".$v['major']
                    ."<br>minor => ".$v['minor']
                    ."<br>comment => ".$v['comment']
                    ."<br>poi_id => ".$v['poi_id'];
            }
        }else{
            echo "errcode => ".$res['errcode']."<br>errmsg => ".$res['errmsg'];
        }
    }
}